package com.ruoyi.zsyh.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 大区会长预订单Controller
 * 
 * @author yuexl
 * @date 2021-12-15
 */
@Controller
@RequestMapping("/zsyh/report")
public class ZsyhAgentReportController extends BaseController
{
    private String prefix = "zsyh/report";

    @Autowired
    private IZsyhAgentOrderService zsyhAgentOrderService;
    @Autowired
    private IZsyhPartsQrcodeApplyService zsyhPartsQrcodeApplyService;
    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;
    @Autowired
    private IZsyhPlatformFactoryService zsyhPlatformFactoryService;
    @Autowired
    private IZsyhPartsQrcodeHistoryService zsyhPartsQrcodeHistoryService;
    @Autowired
    private IZsyhServiceOrderService zsyhServiceOrderService;
    @Autowired
    private IZsyhUserCooperationService zsyhUserCooperationService;
    @Autowired
    private IZsyhElectricOrderService zsyhElectricOrderService;

    @RequiresPermissions("zsyh:agentOrder:view")
    @GetMapping()
    public String agentOrder()
    {
        return prefix + "/agentOrder";
    }

    /**
     * 查询大区会长预订单列表
     */
    @RequiresPermissions("zsyh:report:list")
    @PostMapping("/listReport")
    @ResponseBody
    public AjaxResult listReport()
    {
        ZsyhReport zsyhReport = new ZsyhReport();
        //补助金待审核
        ZsyhPartsQrcodeApply zsyhPartsQrcodeApply = new ZsyhPartsQrcodeApply();
        zsyhPartsQrcodeApply.setApplyStatus("0");
        zsyhReport.setPartsPayReviewNum(zsyhPartsQrcodeApplyService.selectZsyhPartsQrcodeApplyList(zsyhPartsQrcodeApply).size());
        //大区会长预订单
        ZsyhAgentOrder zsyhAgentOrder = new ZsyhAgentOrder();
        Map<String,Object> params = new HashMap<>();
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(d);
        params.put("beginCreateTime",dateNowStr+" 00:00:00");
        params.put("endCreateTime",dateNowStr+" 23:59:59");
        params.put("orderState","0");
        zsyhAgentOrder.setParams(params);
        zsyhReport.setAgentOrderNuM(zsyhAgentOrderService.selectZsyhAgentOrderList(zsyhAgentOrder).size());
        //入驻申请待审核
        ZsyhUserRepairstore zsyhUserRepairstore = new ZsyhUserRepairstore();
        zsyhUserRepairstore.setStoreState("1");
        zsyhReport.setStoreApplyNum(zsyhUserRepairstoreService.selectZsyhUserRepairstoreList(zsyhUserRepairstore).size());
        //合作厂家待审核
        ZsyhPlatformFactory zsyhPlatformFactory = new ZsyhPlatformFactory();
        zsyhPlatformFactory.setFactoryState("0");
        zsyhReport.setFactoryApplyNum(zsyhPlatformFactoryService.selectZsyhPlatformFactoryList(zsyhPlatformFactory).size());
        //产品二维码申请单
        ZsyhPartsQrcodeHistory zsyhPartsQrcodeHistory = new ZsyhPartsQrcodeHistory();
        zsyhPartsQrcodeHistory.setParams(params);
        zsyhReport.setQrcodeApplyNum(zsyhPartsQrcodeHistoryService.selectZsyhPartsQrcodeHistoryList(zsyhPartsQrcodeHistory).size());
        //待处理服务单
        ZsyhServiceOrder zsyhServiceOrder = new ZsyhServiceOrder();
        zsyhServiceOrder.setOrderState("0");
        zsyhReport.setServiceOrderNum(zsyhServiceOrderService.selectZsyhServiceOrderList(zsyhServiceOrder).size());
        //待处理合作申请
        ZsyhUserCooperation zsyhUserCooperation = new ZsyhUserCooperation();
        zsyhUserCooperation.setCooperaState("0");
        zsyhReport.setCooperateApplyNum(zsyhUserCooperationService.selectZsyhUserCooperationList(zsyhUserCooperation).size());
        //待处理整机服务单
        ZsyhElectricOrder zsyhElectricOrder = new ZsyhElectricOrder();
        zsyhElectricOrder.setOrderState("0");
        zsyhReport.setElectrictOrderNum(zsyhElectricOrderService.selectZsyhElectricOrderList(zsyhElectricOrder).size());
        return new AjaxResult(AjaxResult.Type.SUCCESS,"success",zsyhReport);
    }

    /**
     * 查询大区会长预订单列表
     */
    @RequiresPermissions("zsyh:agentOrder:list")
    @PostMapping("/listCurrentMonth")
    @ResponseBody
    public TableDataInfo listCurrentMonth(ZsyhAgentOrder zsyhAgentOrder)
    {
        //获取当前年月
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        zsyhAgentOrder.setCurrentYear(year);
        zsyhAgentOrder.setCurrentMonth(month);
        List<ZsyhAgentOrder> list = zsyhAgentOrderService.selectZsyhAgentOrderCurrentList(zsyhAgentOrder);
        return getDataTable(list);
    }

    /**
     * 查询大区会长预订单列表
     */
    @RequiresPermissions("zsyh:agentOrder:list")
    @PostMapping("/listCurrentYear")
    @ResponseBody
    public TableDataInfo listCurrentYear(ZsyhAgentOrder zsyhAgentOrder)
    {
        //获取当前年月
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        zsyhAgentOrder.setCurrentYear(year);
        List<ZsyhAgentOrder> list = zsyhAgentOrderService.selectZsyhAgentOrderSum(zsyhAgentOrder);
        return getDataTable(list);
    }

    /**
     * 导出大区会长预订单列表
     */
    @RequiresPermissions("zsyh:agentOrder:export")
    @Log(title = "大区会长预订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhAgentOrder zsyhAgentOrder)
    {
        List<ZsyhAgentOrder> list = zsyhAgentOrderService.selectZsyhAgentOrderList(zsyhAgentOrder);
        ExcelUtil<ZsyhAgentOrder> util = new ExcelUtil<ZsyhAgentOrder>(ZsyhAgentOrder.class);
        return util.exportExcel(list, "大区会长预订单数据");
    }

    /**
     * 新增大区会长预订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存大区会长预订单
     */
    @RequiresPermissions("zsyh:agentOrder:add")
    @Log(title = "大区会长预订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhAgentOrder zsyhAgentOrder)
    {
        return toAjax(zsyhAgentOrderService.insertZsyhAgentOrder(zsyhAgentOrder));
    }

    /**
     * 修改大区会长预订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhAgentOrder zsyhAgentOrder = zsyhAgentOrderService.selectZsyhAgentOrderById(id);
        mmap.put("zsyhAgentOrder", zsyhAgentOrder);
        return prefix + "/edit";
    }

    /**
     * 查看详情
     */
    @GetMapping("/viewOrder/{id}")
    public String viewOrder(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhAgentOrder zsyhAgentOrder = zsyhAgentOrderService.selectZsyhAgentOrderById(id);
        mmap.put("zsyhAgentOrder", zsyhAgentOrder);
        return prefix + "/viewOrder";
    }


    /**
     * 修改保存大区会长预订单
     */
    @RequiresPermissions("zsyh:agentOrder:edit")
    @Log(title = "大区会长预订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhAgentOrder zsyhAgentOrder)
    {
        return toAjax(zsyhAgentOrderService.updateZsyhAgentOrder(zsyhAgentOrder));
    }

    /**
     * 删除大区会长预订单
     */
    @RequiresPermissions("zsyh:agentOrder:remove")
    @Log(title = "大区会长预订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zsyhAgentOrderService.deleteZsyhAgentOrderByIds(ids));
    }

}
